package com.keyancompany.zhongbaopingtai.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.IFragmentPagerAdapter;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.fragment.DocsFragment;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.QueryTeamShare;
import com.keyancompany.zhongbaopingtai.model.SharePager;
import com.keyancompany.zhongbaopingtai.utils.DebugLog;
import com.keyancompany.zhongbaopingtai.utils.IntentUtils;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.MyDialog;


import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.BindString;

/**
 * 分档分享区
 */
public class DocumentShareActivity extends BaseActivity implements DocsFragment.OnListFragmentInteractionListener{

    @BindString(R.string.document_share_text)
    String title;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tabLayout)
    TabLayout tabLayout;
    @Bind(R.id.document_viewpager)
    ViewPager documentViewpager;

    private List<SharePager> list;

    public List<SharePager> getList() {
        return list;
    }

    public void setList(List<SharePager> list) {
        this.list = list;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_document_share;
    }

    @Override
    protected int getMenuId() {
        return R.menu.create_share_menu;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                IntentUtils.startActivity(mContext, UploadDocActivity.class);
                return false;
            }
        });
        initData();
    }
    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initTab(){

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(DocsFragment.newInstance(1, 1));
        fragmentList.add(DocsFragment.newInstance(1, 2));
        IFragmentPagerAdapter pagerAdapter = new IFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        pagerAdapter.setTitles(getResources().getStringArray(R.array.document_titles));
        documentViewpager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(documentViewpager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    }
    private void initData() {
        if (isNetvaiable()) {
            String taskId ="";
            String filePid="";//分享文件 Id（可以有可以没有）

            String teamCode = SPUtils.getComment(mContext).getString(Constants.TEAM_CODE, null);
            TeamAPI.queryTeamShare(teamCode,filePid,taskId, DocumentShareActivity.this,
                    getLoading(R.string.watting_hint));
        }
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        switch (type){
            case TeamAPI.QUERY_TEAM_SHARE_MARK:

                if (validateJson(json) != null) {
                    BaseModel base = JSONObject.parseObject(json, BaseModel.class);
                    QueryTeamShare share = JSONObject.parseObject(base.data,QueryTeamShare.class);

                    if (base.result.equals(Constants.SUCESS)){
                        if(share.getPager().size()>0){
                            list=share.getPager();
                            initTab();
                        }else{
                            T.showShort(mContext,"当前暂无分享内容！");
                            return;
                        }
                    } else {
                        T.showShort(mContext, base.msg);
                    }
                }
                break;

            case TeamAPI.DOWNLOAD_MARK:

                if (validateJson(json) != null) {
                    Log.e("TT","文件下载！"+json);
                    BaseModel base = JSONObject.parseObject(json, BaseModel.class);

                        T.showShort(mContext, base.msg);
                 }
                break;
            default:
                break;
                }
            }

    @Override
    public void failed(String error) {
        super.failed(error);
        T.showShort(mContext, error);
    }

    private MyDialog myDialog;
    String pid;
    @Override
    public void onListFragmentInteraction(SharePager pager) {
        pid = pager.getPid();
        myDialog = new MyDialog(this);
        myDialog.setTitle(getResources().getString(R.string.does_download));
        myDialog.setMessage(getResources().getString(R.string.download));
        myDialog.dialog.show();//显示

        myDialog.setMyDialogOnClick(new MyDialog.MyDialogOnClick() {
            @Override
            public void ok() {
                if(isNetvaiable()){
                    TeamAPI.download(pid,DocumentShareActivity.this,getLoading(R.string.watting_hint));
                    DebugLog.i("Start download file: " + pid);
                }

                myDialog.dialog.dismiss();//关闭
            }
        });
    }
}
